<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<title>원무 입원 접수</title>
<script type="text/javascript">
	$(document).ready(function(){
		$("#selectFloor").change(function(){ //층 선택
			var floornum = $("#selectFloor option:selected").val(); //선택한 층 값 저장
			
			//////////////////층 찾는 비동기 시작//////////////////
			$.ajax({ 
				url : 'changefloor.do',
				data : 'floornum=' + floornum, //층을 data로 보냄
				success : function(data){
					$("#wardp").empty();
					$("#wardp").append(floornum);
					$(".room1").remove();

					for(var i = 0; i < data.floorlist.length; i++){
						var html = "";
						var row = data.floorlist[i].max_row; //병실에 침대 가로 수 
						var col = data.floorlist[i].max_col; //병실에 침대 세로 수
						var bedNum = 1;
							
						html += "<div class='room1' id='" + data.floorlist[i].ward_num + "'><b>"+ data.floorlist[i].ward_num + "</b>";
						html += "<table class='wardtb' border='1' style='height:80%;' id='tb" + data.floorlist[i].ward_num + "'>";
						for(var j = 0; j < row; j++){ //침대 가로 수 만큼 반복
							html += "<tr class='row' id='tr" + data.floorlist[i].ward_num + "'>";
							for(var k = 0; k < col; k++){ //침대 세로 수 만큼 반복
								html += "<td class='col' id='td" + data.floorlist[i].ward_num + bedNum + "'>";
								html += bedNum + "</td>";
								bedNum++;
							}
							html += "</tr>"
						}
						html += "</table>";
						html += "</div>";
						$("#info").append(html); 
					}				
					for(var q = 0; q < data.isEmptylist.length; q++){
						for(var i = 0; i < data.floorlist.length; i++){
							var row = data.floorlist[i].max_row; //병실에 침대 가로 수 
							var col = data.floorlist[i].max_col; //병실에 침대 세로 수
							var bedNum = 1;
								
							if(data.isEmptylist[q].ward_Num == data.floorlist[i].ward_num){
								for(var j = 0; j < row; j++){ //침대 가로 수 만큼 반복
									for(var k = 0; k < col; k++){ //침대 세로 수 만큼 반복
										if(data.isEmptylist[q].bed_Num == bedNum){
											$("#td"+data.isEmptylist[q].ward_Num+data.isEmptylist[q].bed_Num).css("background-color", "#D8BFD8");
										}
										bedNum++;
									}
								}
							}
						}
					}
				},
				error : function(data){
					$(".wardp").empty();
					$(".room1").empty();
					alert("층을 선택해주세요");
				}
			});
		});
		
		$(document).on("click", ".col", function(){
			var bedNum = $(this).text();
			var wardNum = $(this).parent().attr("id").substring(2);

			$.ajax({
				url: 'wardPatientInfo.do',
				data: 'wardNum='+wardNum+'&bedNum='+bedNum,
				success: function(data) {			
						var html = "";
						
						$("#info_table").empty();
						for(var i = 0; i < data.wardPatient.length; i++){
							html += "<input type='text' name='floor' value='" + data.wardPatient[i].floor + "'>";
							html += "<input type='hidden' name='ward_Num' value='" + data.wardPatient[i].ward_Num + "'>";
							html += "<input type='hidden' name='bed_Num' value='" + data.wardPatient[i].bed_Num + "'>";
							html += "<input type='hidden' name='name' value='" + data.wardPatient[i].name + "'>";
							html += "<input type='hidden' name='patient_id' value='" + data.wardPatient[i].patient_id + "'>";
							html += "<input type='hidden' name='patient_id' value='${p.patient_id}'>";
						}
						$("#insertward").append(html); 
			  	},
			});
		});	
	});
 $(document).on("click", ".col", function(){
		var bedNum = $(this).text();
		var wardNum = $(this).parent().attr("id").substring(2);
		var patient_id=$("#patient_id").val();
		var is_night = $("#is_night").val();
		var jupsoo_code_id=$("#jupsoo_code_id").val();
		var bohum_code_id=$("#bohum_code_id").val();
		var doctor_id=$("#doctor_id").val();
		var jupsoo_date=$("#jupsoo_date").val();
		var is_holiday=$("#is_holiday").val();
		$('#info_table').val("");
		$.ajax({
			url : "wardinsert.do",
			data: "bedNum="+bedNum+"&wardNum="+wardNum+"&patient_id="+patient_id+"&doctor_id="+doctor_id
			+"&is_night="+is_night+"&jupsoo_code_id="+jupsoo_code_id+"&bohum_code_id="+bohum_code_id
			+"&jupsoo_date="+jupsoo_date+"&is_holiday="+is_holiday,
			
			success : function(data){
				if(data === "yes"){
					alert("성공적으로 입원 되었습니다");
					window.location.href="roominfo.do";
				}else {
					alert("입원 할수 없습니다")
					location.reload();
				}			
			},
			error : function(data){
				alert("입원 할수가 없습니다");
				window.location.href='jupsoo.do';
			}
		});
	}) 
	
</script>
</head>
<body>
	<div id="systembase">
		<div id="info">
			<h3><b id="wardp" class="wardp"></b>층 입원실</h3>
		</div>
	</div>
	<div id="systembaseside" >
		<div class="select">
			<select id="selectFloor">
				<option selected>--층 선택--</option>
				<option>2</option>
				<option>3</option>
				<option>4</option>
				<option>5</option>
				<option>6</option>
				<option>7</option>
			</select>
		</div>
		
		<div >
			<table  id="info_table" >
				
					<tr><td>환자 코드</td><td><input type='text' id='patient_id' name='patient_id' readonly='readonly' value='${jupsoo.patient_id}'></td></tr>
					<tr><td>환자 이름</td><td><input type='text' id='name' name='name' readonly='readonly' value='${p.name }'></td></tr>
					<tr><td>주/야간여부</td><td><input type='text' id='is_night' name='is_night' readonly='readonly' value='${jupsoo.is_night }'></td></tr>
					<tr><td>휴일여부</td><td><input type='text' id='is_holiday' name='is_holiday' readonly='readonly' value='${jupsoo.is_holiday }'></td></tr>
					<tr><td>접수 코드번호</td><td><input type='text' id='jupsoo_code_id' name='jupsoo_code_id' readonly='readonly' value='${jupsoo.jupsoo_code_id}'></td></tr>
					<tr><td>보험 여부</td><td><input type='text' id='bohum_code_id' name='bohum_code_id' readonly='readonly' value='${jupsoo.bohum_code_id}'></td></tr>
					<tr><td>의사</td><td><input type='text' id='doctor_id' name='doctor_id' readonly='readonly' value='${jupsoo.doctor_id}'></td></tr>
					<tr><td>접수 날짜</td><td><input type='text' id='jupsoo_date' name='jupsoo_date' readonly='readonly' value='${jupsoo.jupsoo_date}'></td></tr>
					<tr><td>접수 코드</td><td><input type='text' id='jupsoo_code_id' name='jupsoo_code_id' readonly='readonly' value='${jupsoo.jupsoo_code_id}'></td></tr>		
			</table>
		</div>
		<!-- <div class="button">
			<form id="insertward">
				<button>환자 등록 </button>
			</form>
		</div> -->
</body>
</html>